package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.task.ModifyUserInfoTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.management.ToastManager;
import com.zw.yzk.kabu.view.view.ModifyUserInfoView;
import com.zw.yzk.kabu.view.widget.LoadingDialog;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2017/3/4.
 */

public class ModifyUserInfoPresenter implements Presenter {

    private UseCase modifyUserInfo;
    private AppCompatActivity activity;
    private ModifyUserInfoView modifyUserInfoView;

    @Inject
    public ModifyUserInfoPresenter(@Named("modifyUserInfoUseCase")UseCase modifyUserInfo, AppCompatActivity activity) {
        this.modifyUserInfo = modifyUserInfo;
        this.activity = activity;
    }

    public void setView(ModifyUserInfoView view) {
        this.modifyUserInfoView = view;
    }

    public void modifyUserInfo(String nickName, int sex, String province, String city, String introduction) {
        ModifyUserInfoTask task = new ModifyUserInfoTask();
        task.nickName = nickName;
        task.sex = sex;
        task.province = province;
        task.city = city;
        task.sign = introduction;

        modifyUserInfo.execute(new ModifyUserInfoSubscriber(activity), task);
    }


    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        modifyUserInfo.unSubscribe();
    }

    private class ModifyUserInfoSubscriber extends BaseSubscriber<Boolean> {

        public ModifyUserInfoSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.modify_user_info_failed);
        }

        @Override
        public void onNext(Boolean aBoolean) {
            super.onNext(aBoolean);
            ToastManager.getInstance().showToast(activity, activity.getResources().getString(R.string.modify_success));
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }
    }
}
